not equal, we'll assume that a note was removed. And that is exactly what we'll
do.
If the
removeNote
function returns
true
, that means a note was removed; if it returns
false, that means a note was not removed. In the
removeNotes
function we can add
return, as shown in the following code. We'll check if
notes.length
does not equal
filteredNotes.length:
var removeNote = (title) => { var notes = fetchNotes(); var filteredNotes = notes.filter((note) =>
note.title !== title); saveNotes(filteredNotes);
return notes.length !== filteredNotes.length; };
If they're not equal it will return
true
, which is what we want because a note
was removed. If they're equal it will return
false
, which is great.
Now, inside of
app.js
we can add a few lines in the
removeNote
,
else if
block to make the
output for this command a little nicer. The first thing to do is to store that Boolean.
I'll make a variable called
noteRemoved
and we'll set that equal to the return, result as
shown in the following code, which will either be
true
or
false
:
} else if (command == 'remove') {
var noteRemoved = notes.removeNote(argv.title); }
On the next line, we can create our message, and I'll do this all on one line
using the ternary operator. Now, the ternary operator lets you specify a condition.
In our case, we'll use a var message and it will be set equal to the condition
noteRemoved, which will be
true
if a note was removed and
false
if it wasn't.
Now, the ternary operator can be a little confusing, but it's really
useful inside JavaScript and Node.js. The format for the ternary
operator is first we add the condition, question mark, the truthy
expression to run, colon, and then the falsy expression to run.
After the condition, we'll put a space with a question mark and a space; this is the
statement that will run if it's true. If the
noteRemoved
condition passes, what we want
to do is set message equal to
Note was removed
: var message = noteRemoved ? 'Note was removed' :
Now, if
noteRemoved
is
false
, we can specify that condition right after the colon in the
previous statement. Here, if there is no note removed we'll use the text
Note
not found: var message = noteRemoved ? 'Note was removed' : 'Note not found';